import { defineStore } from 'pinia';
import {getUserInfoApi} from "@/api/user";

export const userStore = defineStore('userStore', {
    state: () => {
        return {
            userId:'',
            nickName:'',
            token:'',
            codeList:[] //权限字段
        }
    }
    ,
    getters: {

        getUserId(state) {
            return state.userId
        },
        getNickName(state) {
            return state.nickName
        },
        getToken(state) {
            return state.token
        },
        getCodeLIst(state){
            return state.codeList
        }
    },
    actions: {
        setUserId(userId: string) {
            this.userId = userId;
        },
        setNickName(nickName: string) {
            this.nickName = nickName;
        },
        setToken(token: string) {
            this.token = token;
        },
        getInfo(){
            return new Promise((resolve, reject)=>{
                getUserInfoApi(this.userId).then((res)=>{
                    if(res && res.code==200){
                        this.codeList = res.data.permissions
                    }
                    resolve(this.codeList)
                }).catch((err)=>{
                    reject(err)
                })
            })
        }
    },
    persist: {
        enabled: true,
        strategies: [
            { paths: ['userId','nickName','token'], storage: localStorage }
        ]
    }

})